package org.opensuse.bs.ui;

import java.io.IOException;
import java.util.HashMap;

import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

public class BSPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
	private HashMap<String, HashMap<String, String>> hm;
	
	private Text[] fields = new Text[5];
	private String[] keys = {"apisrv", "scheme", "su-wrapper", "build-root", "packagecachedir"};
	
	private Group createGroup(Composite parent, String title) {
		Group group = new Group(parent, SWT.NULL);
		group.setText(title);
		GridData gd = new GridData();
		group.setLayoutData(gd);
		gd.grabExcessHorizontalSpace = true;
		group.setLayout(new GridLayout());
		return group;
	}
	
	private Label createLabel(Composite parent, String title) {
		Label label = new Label(parent, SWT.NONE);
		label.setText(title);
		return label;
	}
	
	private Text createText(Composite parent, String message, String key) {
		Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
		text.setToolTipText(message);
		
		if(hm.get("general") != null) {
			String field = hm.get("general").get(key);
			if(field != null) {
				text.setText(field);
			}
		}
		GridData gd = new GridData();
		text.setLayoutData(gd);
		gd.grabExcessHorizontalSpace = true;
		gd.widthHint = 410;
		return text;
	}
	
	protected Control createContents(Composite parent) {
		Composite client = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		client.setLayout(layout);
		
		//connection group
		Group group1 = createGroup(client, "Connection");
		createLabel(group1, "API Source");
		fields[0] = createText(group1, "Default url to connect ot Build Service.", keys[0]);
		createLabel(group1, "Protocal Scheme");
		fields[1] = createText(group1, "Http or Https or Others?", keys[1]);
		
		//build group
		Group group2 = createGroup(client, "Building Package");
		createLabel(group2, "su wrapper");
		fields[2] = createText(group2, "Wrapper to call build as root" +
				" (sudo, su -, ...)", keys[2]);
		createLabel(group2, "build root");
		fields[3] = createText(group2, "rootdir to setup the chroot environment", keys[3]);
		createLabel(group2, "package cache");
		fields[4] = createText(group2, "Downloaded packages are" +
				"cached here. Must be writable to you.", keys[4]);
		
		return client;
	}

	public void init(IWorkbench workbench) {
		hm = BSUIPlugin.getDefault().getSections();
	}
	
	public boolean performOk() {
		HashMap<String, String> section;
		boolean dirty = false;
		if(hm.get("general") == null) {
			section = new HashMap<String, String>();
			for(int i=0;i<keys.length;i++) {
				String field = fields[i].getText().trim();
				if(field.length() != 0) {
					section.put(keys[i], field);
				}
			}
			if(!section.isEmpty()) {
				hm.put("general", section);
				dirty = true;
			}
		}
		else {
			section = hm.get("general");
			boolean fc = false;
			for(int i=0;i<keys.length;i++) {
				String field = fields[i].getText().trim();
				String old = section.get(keys[i]);
				if((old == null && field.length() == 0) || field.equals(old)) {
					continue;
				}
				fc = true;
				if(field.length() == 0) {
					section.remove(keys[i]);
				}
				else {
					section.put(keys[i], field);
				}
			}
			if(fc) {
				if(section.isEmpty()) {
					hm.remove("general");
				}
				else {
					hm.put("general", section);
				}
				dirty = true;
			}
		}
		if(dirty) {
			try {
				OSC.writeback(hm);
			}catch(IOException e) {
				System.out.println(e.getMessage());
			}
		}
		return true;
	}
	
	public void performDefaults() {
		if(hm.get("general") == null) {
			for(int i=0;i<fields.length;i++) {
				fields[i].setText("");
			}
		}
		else {
			for(int i=0;i<keys.length;i++) {
				String field = hm.get("general").get(keys[i]);
				if(field == null) {
					fields[i].setText("");
				}
				else {
					fields[i].setText(field);
				}
			}
		}
	}

}
